import java.awt.Graphics;
import java.awt.GraphicsConfiguration;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.filechooser.FileNameExtensionFilter;

/**
 * Generated with window builder with a few edits <br />
 * Screen for reformating tex references and saving them into another bibliography part.
 * @author cerko, mydlo
 * 
 */
public class ReformatTexView extends JFrame {
	
	private static int windowWidth = 800;
	private static int windowHeight = 630;
	private static String backgroundPath = "img/screen/reformateScreen.jpg";

	private JPanel contentPane;
	
	private JPanel backgroundPanel;
	
	private JTextField TeXInputField;
	private JTextField inputFormatField;
	private JTextField teXOutputField;
	private JTextField formatOutputField;
	
	private File texInput;
	private File texOutput;
	private File inputFormat;
	private File formatOutput;
	private File formatInput;
	private JButton btnStart;
	
	private JFrame frame;
	private static JFrame home;

	/**
	 * Create the frame.
	 */
	public ReformatTexView(JFrame home) {
		ReformatTexView.home = home;
		initialize();
	}
	
	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		
		frame = this;
		
		setBounds(100, 100, 450, 300);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setResizable(false);
		
		setSize(windowWidth, windowHeight);

		GraphicsConfiguration gc = getGraphicsConfiguration();
		Rectangle bounds = gc.getBounds();

		setLocation(
				(int) ((bounds.width / 2) - (windowWidth / 2)),
				(int) ((bounds.height / 2) - (windowHeight / 2))
				);
		
		//test svn
		//magic to make background work
		backgroundPanel = new JPanel(){
			@Override
			 public void paintComponent(Graphics g){
				Image img = null;
				try {
					img = ImageIO.read(new File(backgroundPath));
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
                g.drawImage(img, 0, 0, null);
            } 
		};
		
		getContentPane().setLayout(createFromBibTexView());
	}
	
	/**
	 * vytvory cely layout
	 * @return hotovy layout
	 */
	public GroupLayout createFromBibTexView(){
		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.TRAILING)
				.addComponent(backgroundPanel, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 392, Short.MAX_VALUE)
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.TRAILING)
				.addComponent(backgroundPanel, GroupLayout.DEFAULT_SIZE, 370, Short.MAX_VALUE)
		);
		
		JPanel panel = new JPanel();
		panel.setBounds(0, 0, 794, 602);
		//nastavenie priesvitneho panelu
		panel.setOpaque(false);
		
		inputFormatField = new JTextField();
		inputFormatField.setBounds(310, 310, 177, 14);
		inputFormatField.setColumns(10);
		inputFormatField.setOpaque(false);
		inputFormatField.setBorder(BorderFactory.createEmptyBorder());
		
		TeXInputField = new JTextField();
		TeXInputField.setBounds(310, 395, 177, 14);
		TeXInputField.setColumns(10);
		TeXInputField.setOpaque(false);
		TeXInputField.setBorder(BorderFactory.createEmptyBorder());
		
		JButton btnTeXInput = new JButton(new ImageIcon("img/buttons/browseButton.png"));
		btnTeXInput.setBounds(494, 386, 85, 30);
		
		btnTeXInput.setBorder(BorderFactory.createEmptyBorder());
		btnTeXInput.setOpaque(false);
		btnTeXInput.setContentAreaFilled(false);
		
		btnTeXInput.addMouseListener(new ButtonMouseListener(new ImageIcon("img/buttons/browseButtonA.png"), new ImageIcon("img/buttons/browseButton.png"), btnTeXInput));
		
		JButton btnBiBInput = new JButton(new ImageIcon("img/buttons/browseButton.png"));
		btnBiBInput.setBounds(494, 302, 85, 30);
		
		btnBiBInput.setBorder(BorderFactory.createEmptyBorder());
		btnBiBInput.setOpaque(false);
		btnBiBInput.setContentAreaFilled(false);
		
		btnBiBInput.addMouseListener(new ButtonMouseListener(new ImageIcon("img/buttons/browseButtonA.png"), new ImageIcon("img/buttons/browseButton.png"), btnBiBInput));
		
		btnBiBInput.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				JFileChooser fileChooser = new JFileChooser();
				fileChooser.setFileFilter(new FileNameExtensionFilter("input format (.txt)", "txt"));
				int returnVal = fileChooser.showOpenDialog(frame);
				if (returnVal == JFileChooser.APPROVE_OPTION){
					inputFormat = fileChooser.getSelectedFile();
					inputFormatField.setText(inputFormat.getAbsolutePath());
				}
			}
		});
		
		teXOutputField = new JTextField();
		teXOutputField.setBounds(310, 435, 177, 14);
		teXOutputField.setColumns(10);
		teXOutputField.setOpaque(false);
		teXOutputField.setBorder(BorderFactory.createEmptyBorder());
		
		JButton btnTexOutput = new JButton(new ImageIcon("img/buttons/browseButton.png"));
		btnTexOutput.setBounds(495, 426, 85, 30);
		
		btnTexOutput.setBorder(BorderFactory.createEmptyBorder());
		btnTexOutput.setOpaque(false);
		btnTexOutput.setContentAreaFilled(false);
		
		btnTexOutput.addMouseListener(new ButtonMouseListener(new ImageIcon("img/buttons/browseButtonA.png"), new ImageIcon("img/buttons/browseButton.png"), btnTexOutput));
		
		btnTexOutput.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				JFileChooser fileChooser = new JFileChooser();
				fileChooser.setFileFilter(new FileNameExtensionFilter("TeX file (.tex)", "tex"));
				int returnVal = fileChooser.showSaveDialog(frame);
				if (returnVal == JFileChooser.APPROVE_OPTION){
					teXOutputField.setText(fileChooser.getSelectedFile().getAbsolutePath());
					if (!teXOutputField.getText().endsWith(".tex")){
						teXOutputField.setText(teXOutputField.getText() + ".tex");
					}
				}
			}
		});
		
		btnStart = new JButton(new ImageIcon("img/buttons/startMaleButton.png"));
		btnStart.setBounds(365, 465, 70, 70);
		
		btnStart.setBorder(BorderFactory.createEmptyBorder());
		btnStart.setOpaque(false);
		btnStart.setContentAreaFilled(false);
		
		btnStart.addMouseListener(new ButtonMouseListener(new ImageIcon("img/buttons/startMaleButtonA.png"), new ImageIcon("img/buttons/startMaleButton.png"), btnStart));
		
		btnStart.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				texOutput = new File(teXOutputField.getText());
				new Dispatcher().reformatRefs(texInput, texOutput, inputFormat, formatOutput);
			}
		});
		backgroundPanel.setLayout(null);
		
		formatOutputField = new JTextField();
		formatOutputField.setBounds(310, 354, 177, 14);
		formatOutputField.setColumns(10);
		formatOutputField.setOpaque(false);
		formatOutputField.setBorder(BorderFactory.createEmptyBorder());
		
		JButton btnFormatinput = new JButton(new ImageIcon("img/buttons/browseButton.png"));
		btnFormatinput.setBounds(494, 345, 85, 30);
		btnFormatinput.setBorder(BorderFactory.createEmptyBorder());
		btnFormatinput.setOpaque(false);
		btnFormatinput.setContentAreaFilled(false);
		
		btnFormatinput.addMouseListener(new ButtonMouseListener(new ImageIcon("img/buttons/browseButtonA.png"), new ImageIcon("img/buttons/browseButton.png"), btnFormatinput));
		
		btnFormatinput.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				JFileChooser fileChooser = new JFileChooser();
				fileChooser.setFileFilter(new FileNameExtensionFilter("txt file (.txt)", "txt"));
				int returnVal = fileChooser.showOpenDialog(frame);
				if (returnVal == JFileChooser.APPROVE_OPTION){
					formatOutput = fileChooser.getSelectedFile();
					formatOutputField.setText(formatOutput.getAbsolutePath());
				}
			}
		});
		
		btnTeXInput.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				JFileChooser fileChooser = new JFileChooser();
				fileChooser.setFileFilter(new FileNameExtensionFilter("tex files (.tex)", "tex"));
				int returnVal = fileChooser.showOpenDialog(frame);
				if (returnVal == JFileChooser.APPROVE_OPTION){
					texInput = fileChooser.getSelectedFile();
					TeXInputField.setText(texInput.getAbsolutePath());
				}
			}
		});
		
		JButton homeButton = new JButton(new ImageIcon("img/buttons/homeButton.png"));
		
		homeButton.addMouseListener(new ButtonMouseListener(new ImageIcon("img/buttons/homeButtonA.png"), new ImageIcon("img/buttons/homeButton.png"), homeButton));
		
		homeButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				setVisible(false);
				ReformatTexView.home.setVisible(true);
			}
		});
		homeButton.setBounds(38, 513, 52, 52);
		homeButton.setBorder(BorderFactory.createEmptyBorder());
		homeButton.setOpaque(false);
		homeButton.setContentAreaFilled(false);
		
		backgroundPanel.add(panel);
		panel.setLayout(null);
		panel.add(inputFormatField);
		panel.add(TeXInputField);
		panel.add(teXOutputField);
		panel.add(formatOutputField);
		panel.add(btnFormatinput);
		panel.add(btnTexOutput);
		panel.add(btnBiBInput);
		panel.add(btnTeXInput);
		panel.add(btnStart);
		panel.add(homeButton);
		
		return groupLayout;
	}
	
}
